/*
https://leetcode-cn.com/problems/orderly-queue/solution/you-xu-dui-lie-by-leetcode/
 */
import java.util.Arrays;

public class Solution899 {
    public String orderlyQueue(String s, int k) {
        if (k>1){
            char[] chars=s.toCharArray();
            Arrays.sort(chars);
            return String.valueOf(chars);
        }else{
            String min=s;
            String cur=s;
            for (int i=0;i<s.length();i++){
                String temp=cur.substring(1).concat(cur.substring(0,1));
                if (min.compareTo(temp)>0){
                    min=temp;
                }
                cur=temp;
            }
            return min;
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution899().orderlyQueue("baaca",3));
    }
}
